// 俄罗斯方块语言切换
function handleClick(param) {
	//document.getElementById("tetris").src = "https://hmzhang_jin_peng.gitee.io/tetris-games/?lan=" + param;
	// 获取链接元素，修改链接的 href 属性
	document.getElementById("tetris").href = "https://qq1908077730.gitlab.io/tetris/?lan=" + param;
}

var skills = document.getElementById('skills');

var iframe1 = document.getElementById('myIframe');
var iframe2 = document.getElementById('fcgame');

var resource = document.getElementById('resource');
var services = document.getElementById('services');
var software = document.getElementById('software');
var game = document.getElementById('game');
var baiduMaps = document.getElementById('baiduMaps');


function isInPartialViewport(element,triggerHeight) {
	var rect = element.getBoundingClientRect();
	var windowHeight = window.innerHeight-triggerHeight || document.documentElement.clientHeight-triggerHeight;
	var windowWidth = window.innerWidth || document.documentElement.clientWidth;

	var partialHeight = Math.min(rect.bottom, windowHeight) - Math.max(rect.top, 0);
	var partialWidth = Math.min(rect.right, windowWidth) - Math.max(rect.left, 0);

	var partialArea = partialHeight * partialWidth;
	var elementArea = rect.height * rect.width;

	return (partialArea / elementArea) >= 0; // Adjust the threshold as per your requirement
}

// 载入chatGPT
function loadIframeSrc(iframe) {
	var src = iframe.getAttribute('data-src');
	iframe.src = src;
}

var isIframe1Load = false;
var isbaiduMapsLoad = false;

window.addEventListener('scroll', function() {
	
	if (isInPartialViewport(iframe1,0)) {
		if(!isIframe1Load){
			loadIframeSrc(iframe1);
		}
		isIframe1Load = true;
		//只触发一次
		//window.removeEventListener('scroll', arguments.callee);
	}
	
	
	if (isInPartialViewport(iframe1,0)) {
		if(!isIframe1Load){
			loadIframeSrc(iframe1);
		}
		isIframe1Load = true;
		//只触发一次
		//window.removeEventListener('scroll', arguments.callee);
	}
	
	if(isInPartialViewport(skills,600) && pages.iframe1CurrentPage){
		// 找到要修改的元素
		var pioDialog = document.querySelector('.pio-dialog');
		// 移除对话框
		pioDialog.classList.remove('active');
	}
	
	if (isInPartialViewport(iframe1,500)) {
		//设置其他页面为false
		setSingleTrueProperty(pages, "iframe1CurrentPage")
		//window.removeEventListener('scroll', arguments.callee);
		contentPio("这里是Ai人工智能助手，欢迎你提问！");
	}
	if (isInPartialViewport(resource,600) && !pages.resourceCurrentPage) {
		//设置其他页面为false
		setSingleTrueProperty(pages, "resourceCurrentPage")
		//window.removeEventListener('scroll', arguments.callee);
		contentPio("欢迎浏览鹏peng的资源分享");
	}
	if (isInPartialViewport(software,600) && !pages.softwareCurrentPage) {
		//设置其他页面为false
		setSingleTrueProperty(pages, "softwareCurrentPage")
		//window.removeEventListener('scroll', arguments.callee);
		contentPio("欢迎浏览鹏peng的软件分享");
	}
	if (isInPartialViewport(game,600) && !pages.gameCurrentPage) {
		//设置其他页面为false
		setSingleTrueProperty(pages, "gameCurrentPage")
		//window.removeEventListener('scroll', arguments.callee);
		contentPio("欢迎浏览鹏peng的游戏");
	}
	if (isInPartialViewport(baiduMaps,100)) {
		if(!isbaiduMapsLoad){
			initMap();//创建和初始化地图
		}
		isbaiduMapsLoad = true;
		//只触发一次
		//window.removeEventListener('scroll', arguments.callee);
	}
	if (isInPartialViewport(baiduMaps,600) && !pages.baiduMapsCurrentPage) {
		//设置其他页面为false
		setSingleTrueProperty(pages, "baiduMapsCurrentPage")
		//window.removeEventListener('scroll', arguments.callee);
		contentPio("欢迎浏览百度地图，找找你所在的位置吧！");
	}
});

// 定义一个对象来包含所有页面状态
var pages = {
	iframe1CurrentPage: false,
    resourceCurrentPage: false,
    servicesCurrentPage: false,
    softwareCurrentPage: false,
    gameCurrentPage: false,
    baiduMapsCurrentPage: false
};

// 定义一个通用方法来设置一个属性为 true，其他属性为 false
function setSingleTrueProperty(obj, trueProperty) {
    for (var prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            obj[prop] = (prop === trueProperty);
        }
    }
}



// window.addEventListener('scroll', function() {
// 	if (isInPartialViewport(iframe2)) {
// 	loadIframeSrc(iframe2);
// 	window.removeEventListener('scroll', arguments.callee);
// 	}
// });


// 页面加载时执行
document.addEventListener('DOMContentLoaded', function() {
	var ua = window.navigator.userAgent.toLowerCase();
	ua = ua.indexOf("mobile") || ua.indexOf("android") || ua.indexOf("ios");
	var video = document.getElementById('videoZhuye');
	// 判断是否为移动端，电脑端加载视频
	if(ua === -1){
		video.src = 'assets/video/zhuye.mp4';
		video.autoplay = true;
		video.loop = true;
		video.muted = true;
		video.play();
	}else{
		video.parentNode.removeChild(video);
	}
	
});